﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
using System.Windows;
using System.Reflection;
using System.IO;


namespace Radar
{
    public partial class MainForm : Form
    {
        Radar _radar;
        Coordinate td1 = new Coordinate(1, 400, 400, 4000, 1000, "abc");
        Coordinate td2 = new Coordinate(2, 200, 200, 4000, 1000, "abc");
        List<Coordinate> qd1 = new List<Coordinate>();
        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();

        int speed = 10;
        public static List<FlyingItem> listTarget = new List<FlyingItem>();
        public static List<Rocket> listTenlua = new List<Rocket>();
        public static List<FlyingItem> listItem = new List<FlyingItem>();
        public static ListView lvTarget = new ListView();
        public static float culy;
        public static float phuongvi;
        string pathicon=string.Empty;
        private bool Updatevitri;

        public MainForm()
        {
            InitializeComponent();
            t.Interval = 3600;
            t.Tick += new EventHandler(t_Tick);
            t.Enabled = true;
            backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(backgroundWorker1_DoWork);

        }

        void t_Tick(object sender, EventArgs e)
        {
            double max_angle = _radar.maxAngle * 0.0174532925d;

            if (listTarget.Count == 0)
            {
                //lvTarget.Items.Clear();
                //cbTarget.Items.Clear();
                //cbTarget.Text = "";
            }
            else
            {
                //lvTarget.Items.Clear();
                //cbTarget.Items.Clear();
                // select which of the three items to update
                foreach (FlyingItem item in listTarget)
                {

                    //khoang cach di duoc cua muc tieu trong 1 s
                    double s = item._current.velocity * 1000 / (360000);
                    //double s = item._current.velocity / (3600);

                    //toa do tuong doi giua diem di va diem den
                    double x = item._to.X - item._current.X;
                    double y = item._to.Y - item._current.Y;
                    double chenhlech_kc = Math.Sqrt(x * x + y * y);
                    double gocngang = Math.Acos(x / chenhlech_kc);
                    //doi sang do.
                    gocngang = gocngang / 0.0174532925d;

                    if (y < 0)
                    {
                        gocngang = 360d - gocngang;
                    }
                    //doi sang radian de tinh sin va cos
                    gocngang *= 0.0174532925d;
                    double z = item._to.Z - item._current.Z;
                    double gocdung = Math.Asin(z / Math.Sqrt(z * z + chenhlech_kc * chenhlech_kc));
                    //toa do muc tieu sau 1s
                    double z1 = item._current.Z + (s * Math.Sin(gocdung));
                    double chenhlech_kc1 = (float)(s * Math.Abs(Math.Cos(gocdung)));

                    double x1 = item._current.X + chenhlech_kc1 * Math.Cos(gocngang);
                    double y1 = item._current.Y + chenhlech_kc1 * Math.Sin(gocngang);
                    Coordinate current = new Coordinate(item._current.stt, x1, y1, z1, item._current.velocity, "abc");
                    //gan diem nay thanh diem bat dau
                    item._current = current;
                    //_radar.AddItem(item);

                    //_radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
                    if (_radar._az + 20 > Radar.getAzimuth(current) && _radar._az - 20 < Radar.getAzimuth(current) && Radar.getRange(current) <= pictureBox1.Width / 2)// && (Radar.getRange(item._current) * Math.Sin(max_angle)) > item._current.Z)
                    {
                        bool Flag = true;
                        for (int j = 0; j < lvTarget.Items.Count; j++)
                        {
                            if (lvTarget.Items[j].Text.Trim() == item.ID.ToString().Trim())
                            {
                                ListViewItem lvItem = lvTarget.Items[j];
                                lvItem.Text = item.ID.ToString();
                                lvItem.SubItems[1].Text = (item._category);
                                lvItem.SubItems[2].Text = (Radar.getAzimuth(item._current).ToString());

                                int rg = (int)Radar.getRange(item._current);

                                lvItem.SubItems[3].Text = (rg.ToString());
                                lvItem.SubItems[4].Text = (item._current.Z.ToString());
                                lvItem.SubItems[5].Text = (item._current.velocity.ToString());
                                Flag = false;
                                if (Updatevitri)
                                {
                                    txtoado.Text = Math.Round(item._current.Y).ToString();
                                    txtkm.Text = Math.Round(item._current.X).ToString();
                                    txtdocao.Text = Math.Round(item._current.Z).ToString();
                                    txtvantoc.Text = (item._current.velocity.ToString());
                                    txthuongtoi.Text = Convert.ToInt16(getAzimuth(Convert.ToInt16(item._to.X), Convert.ToInt16(item._to.Y), pictureBox1.Width)).ToString();
                                    vitri.Text = rg.ToString();
                                }
                                _radar.AddItem(item);
                                //_radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
                            }
                        }
                        if (Flag)
                        {
                            ListViewItem lvItem = new ListViewItem();
                            lvItem.Text = item.ID.ToString();
                            lvItem.SubItems.Add(item._category);
                            lvItem.SubItems.Add(Radar.getAzimuth(item._current).ToString());

                            int rg = (int)Radar.getRange(item._current);

                            lvItem.SubItems.Add(rg.ToString());
                            lvItem.SubItems.Add(item._current.Z.ToString());
                            lvItem.SubItems.Add(item._current.velocity.ToString());
                            lvItem.BackColor = item._color;
                            lvTarget.Items.Add(lvItem);
                        }

                        //if (Radar.getRange(current) <= 60)
                        //{
                        //    System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
                        //    myPlayer.SoundLocation = @"c:\sound.wav";
                        //    myPlayer.Play();

                        //}

                    }
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
            myPlayer.SoundLocation = @"c:\sound1.wav";
            myPlayer.Play();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            pathicon = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
            
            _radar = new Radar(pictureBox1.Width, 0, 0, 0, 3);
            _radar.pathIcon = pathicon + @"\Icon\maybay.png";
            pictureBox1.Image = _radar.Image;
            _radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
            _radar.DrawScanInterval = speed;
            //txtMaxAngle.Text = _radar.maxAngle.ToString();

            //trackBarSpeed.Maximum = 60;
            //trackBarSpeed.TickFrequency = 1;
        }

        void _radar_ImageUpdate(object sender, ImageUpdateEventArgs e)
        {
            pictureBox1.Image = e.Image;
            txtNoSpiral.Text = _radar._az.ToString();
        }

        //private void trackBarSpeed_Scroll(object sender, EventArgs e)
        //{
        //    if (trackBarSpeed.Value == 0) trackBarSpeed.Value = 1;
        //    _radar.DrawScanInterval = (speed) / trackBarSpeed.Value;
        //    _radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
        //}

        private void btTieudiet_Click(object sender, EventArgs e)
        {
            //if (cbTarget.Text == "")
            //{ return; }
            //Rocket tenlua1 = new Rocket(Convert.ToInt16(cbTarget.Text));
            //listTenlua.Add(tenlua1);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int _toadoX =Convert.ToInt16(txt_km.Text);
            int _toadoY = Convert.ToInt16(txt_toado.Text);
            int _docao = Convert.ToInt16(txt_Docao.Text);
            int _vantoc = Convert.ToInt16(txt_huongtoi.Text);
            int _huongtoi = Convert.ToInt16(txt_vantoc.Text);

            PointF huong = AzRg2XY(_huongtoi);

            Coordinate td1 = new Coordinate(1, _toadoX, _toadoY, _docao, _vantoc, "abc");
            Coordinate td2 = new Coordinate(2, huong.X, huong.Y, _docao, _vantoc, "abc");

            List<Coordinate> qd = new List<Coordinate>();
            qd.Add(td1);
            qd.Add(td2);
            FlyingItem mb = new FlyingItem("Quân sự (địch)", 5, qd, Color.Red);
            MainForm.listTarget.Add(mb);

        }

        public PointF AzRg2XY(int azimuth)
        {
            int _size = pictureBox1.Width;
            //chuyen goc phuong vi sang goc theo truc X
            double angle = (270d + (double)azimuth);

            // chuyen sang radian
            angle *= 0.0174532925d;

            double r, x, y;

            // tinh x va y theo size cua man hinh scan radar co san
            r = _size*0.5;

            _size = pictureBox1.Width;

            x = (((double)_size * 0.5d) + (r * Math.Cos(angle)));
            y = (((double)_size * 0.5d) + (r * Math.Sin(angle)));

            return new PointF((float)x, (float)y);
        }

        //void txtMaxAngle_LostFocus(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        double max_angle = Convert.ToDouble(txtMaxAngle.Text);
        //        if (max_angle <= 3 || max_angle > 60)
        //        {
        //            MessageBox.Show("Góc nâng của radar không đúng");
        //            return;
        //        }
        //        else
        //            _radar.maxAngle = max_angle;
        //    }
        //    catch
        //    {
        //        MessageBox.Show("Góc nâng của radar không đúng");
        //        return;
        //    }

        //}

        //lay vi tri chuot so voi tam(0,0)
        public static double getRange(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            return canh_huyen/8;
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            int abc = checkmousemove(e.Location.X, e.Location.Y, pictureBox1.Width);


            if (abc <= pictureBox1.Width/2)
           {
               int ab = getAzimuth(e.Location.X, e.Location.Y, pictureBox1.Width);
               toado.Text = "Cự ly: " + e.Location.X.ToString() + " Phương vị " + e.Location.Y.ToString();
               huong.Text = "hướng: " + ab.ToString();
               label6.Text = "vi tri so voi tam: " + Convert.ToInt16(getRange(e.Location.X, e.Location.Y, pictureBox1.Width)).ToString();
           }
           else
           {
               toado.Text = "Cự ly: 0";
               huong.Text = "hướng: 0";
               label6.Text = "vi tri so voi tam: 0";
           }
        }

        private int checkmousemove(int _x,int _y,int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            
            return Convert.ToInt16(canh_huyen);
        }
        private static double DegreesToRadians(double degrees)
        {
            return degrees * Math.PI / 180.0;
        }
        public int getAzimuth(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            //tinh goc so voi truc x
            double angle = Math.Acos(x / canh_huyen);
            //doi sang do.
            angle = angle / 0.0174532925d;

            if (y < 0)
            {
                angle = 360d - angle;
            }
            //chuyen sang goc so voi truc. y - Phuong BAC'

            double azimuth = angle - 270d;
            if (azimuth < 0) azimuth = azimuth + 360d;
            return (int)azimuth;
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            bool aa = _radar.checkMap(e.Location.X, e.Location.Y);
            if (aa)
            {
                //MessageBox.Show("good");
                //_radar.pathIcon =pathicon+ @"\Resources\Icon1.ico";
                Updatevitri = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //khoa muc tieu
            button3.BackColor = Color.Blue;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int km = Convert.ToInt16(txt_km.Text);
            int _toado = Convert.ToInt16(txt_toado.Text);
            int _docao = Convert.ToInt16(txt_Docao.Text);
            int _huongtoi = Convert.ToInt16(txt_huongtoi.Text);
            int _vantoc = Convert.ToInt32(txt_vantoc.Text);

            PointF toado = AzRg2XY(_toado);
            Coordinate td1 = new Coordinate(1, toado.X, toado.Y, _docao, _vantoc, "abc");
            PointF huong = AzRg2XY(_huongtoi);
            Coordinate td2 = new Coordinate(2, huong.X, huong.Y, _docao, _vantoc, "abc");

            List<Coordinate> qd = new List<Coordinate>();
            qd.Add(td1);
            qd.Add(td2);
            FlyingItem mb = new FlyingItem("Quân sự (địch)", 5, qd, Color.Red);
            MainForm.listTarget.Add(mb);
        }
    }
}
